package reposetery

import (
	"Redbook/redbook/internal/Domain"
	"Redbook/redbook/internal/reposetery/dao"
	"context"
	"math/rand"
	"time"
)

type ArticleRepo interface {
	Creat(ctx context.Context, art Domain.Articl) (int, error)
	Update(ctx context.Context, art Domain.Articl) error
	Save(ctx context.Context, articl Domain.Articl) error
}
type articlerepo struct {
	Dao dao.ArticleDao
}

func NewRepo(dao dao.ArticleDao) ArticleRepo {
	return &articlerepo{
		Dao: dao,
	}
}
func (a *articlerepo) Save(ctx context.Context, art Domain.Articl) error {
	return a.Dao.Save(ctx, dao.Article{
		ID:      art.ID,
		Content: art.Content,
		Writter: art.Writter,
		Ctime:   art.Ctime,
		Utime:   time.Now().Unix(),
	})
}
func (a *articlerepo) Creat(ctx context.Context, art Domain.Articl) (int, error) {
	id := rand.Int()
	art.ID = id
	return a.Dao.Insert(ctx, dao.Article{
		ID:      art.ID,
		Title:   art.Title,
		Content: art.Content,
		Writter: art.Writter,
	})
}

func (a *articlerepo) Update(ctx context.Context, art Domain.Articl) error {
	return a.Dao.Update(ctx, dao.Article{
		ID:      art.ID,
		Title:   art.Title,
		Content: art.Content,
		Utime:   time.Now().Unix(),
		Writter: art.Writter,
	})
}
